package morphage.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;

import sun.awt.image.ToolkitImage;

import morphage.engine.ImageManager;
import morphage.engine.Pipeline;

public class AppToolbar extends JPanel implements ActionListener
{
  private static final long serialVersionUID = 1L;

  private static final Dimension ICON_DIMENSION = new Dimension(35, 35);
  private final JButton m_playButton = new JButton();

  private ImageIcon playIcon;

  public AppToolbar()
  {
    // --------------
    // ACTION PANEL
    // --------------
    setLayout(new BorderLayout());
    JPanel actionPanel = new JPanel();
    try
    {
      getResources();

      m_playButton.setIcon(playIcon);
      m_playButton.setBorderPainted(false);
      m_playButton.setBackground(new Color(0, 0, 0, 0));
      m_playButton.setPreferredSize(ICON_DIMENSION);
      // playButton.setBorder(null);

    } catch (Exception ex)
    {

    }
    m_playButton.addActionListener(this);
    actionPanel.add(m_playButton);

    add("West", actionPanel);
  }

  public void getResources() throws Exception
  {
    playIcon = new ImageIcon(ResourceManager.getImage("res/play.jpg"));
  }

  public void actionPerformed(java.awt.event.ActionEvent e)
  {
    if( e.getSource().equals(m_playButton) )
    {
      Image firstImage = ImageManager.getFirstImage();
      Image lastImage  = ImageManager.getLastImage();
      if( firstImage == null || lastImage == null )
        return;
      Pipeline pipe = Pipeline.getDefaultPipeline();
      
      BufferedImage firstBufImage = ((ToolkitImage)firstImage).getBufferedImage();
      BufferedImage lastBufImage = ((ToolkitImage)lastImage).getBufferedImage();
      
      Image result = pipe.Compute(firstBufImage, lastBufImage);
      assert( result != null ); 
      ImageManager.setCurrentResult( result );
      AppCenterPanel.update();
    }
  }
}
